<script setup>
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userRegisterService } from '@/apis/user'

// 表单数据对象
const formData = ref({
  userName: '',
  nickName: '',
  password: '',
  confirmPassword: '',
  agree: false
})

const loading = ref(false)
const formRef = ref()
const router = useRouter()

// 表单校验规则
const rules = {
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
  ],
  nickName: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { min: 2, max: 12, message: '长度在 2 到 12 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== formData.value.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  agree: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('请同意用户协议'))
        } else {
          callback()
        }
      },
      trigger: 'change'
    }
  ]
}

// 注册处理
const doRegister = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await userRegisterService({
          userName: formData.value.userName,
          nickName: formData.value.nickName,
          password: formData.value.password
        })
        if (res.code === 200) {
          ElMessage.success('注册成功')
          router.push('/login')
        } else {
          ElMessage.error(res.msg || '注册失败')
        }
      } catch (error) {
        ElMessage.error('注册失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<template>
  <div class="login-page">
    <header class="login-header">
      <div class="container">
        <h1 class="logo">
          <RouterLink to="/">文创商城</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          返回首页
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="login-container">
        <h2 class="login-title">新用户注册</h2>
        <div class="login-form">
          <el-form
            ref="formRef"
            :model="formData"
            :rules="rules"
            label-position="top"
            status-icon
          >
            <el-form-item label="用户名" prop="userName">
              <el-input
                v-model="formData.userName"
                placeholder="请输入用户名"
                prefix-icon="User"
              />
            </el-form-item>
            <el-form-item label="昵称" prop="nickName">
              <el-input
                v-model="formData.nickName"
                placeholder="请输入昵称"
                prefix-icon="User"
              />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="formData.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
              />
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input
                v-model="formData.confirmPassword"
                type="password"
                placeholder="请确认密码"
                prefix-icon="Lock"
                show-password
              />
            </el-form-item>
            <el-form-item prop="agree" class="agree-item">
              <el-checkbox v-model="formData.agree">
                我已阅读并同意<a href="javascript:;">《用户协议》</a>和<a
                  href="javascript:;"
                  >《隐私政策》</a
                >
              </el-checkbox>
            </el-form-item>
            <el-button
              type="primary"
              class="login-btn"
              :loading="loading"
              @click="doRegister"
            >
              立即注册
            </el-button>
          </el-form>
          <div class="login-extra">
            <div class="login-link">
              已有账号？<RouterLink to="/login">立即登录</RouterLink>
            </div>
          </div>
        </div>
      </div>
    </section>
    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于文创</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">艺术资讯</a>
          <a href="javascript:;">文创合作</a>
          <a href="javascript:;">设计师入驻</a>
          <a href="javascript:;">文创活动</a>
        </p>
        <p>CopyRight &copy; 2024 文创在线商城 版权所有</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
// 复用登录页面的样式
.login-page {
  min-height: 100vh;
  background: #f9f6f2;
}

.login-header {
  background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);

  .container {
    max-width: 1240px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80px;
  }

  .logo {
    width: 200px;
    a {
      display: block;
      height: 40px;
      width: 100%;
      text-indent: -9999px;
      background: url('@/assets/images/logo.png') no-repeat center / contain;
    }
  }

  .entry {
    color: #666;
    transition: color 0.2s;
    &:hover {
      color: #8b4513;
    }
  }
}

.login-section {
  padding: 60px 0;
  position: relative;

  .login-container {
    width: 400px;
    margin: 0 auto;
    background: #fff;
    border-radius: 12px;
    padding: 40px;
    box-shadow: 0 4px 20px rgba(139, 69, 19, 0.1);
  }

  .login-title {
    font-size: 26px;
    text-align: center;
    margin-bottom: 40px;
    color: #8b4513;
    font-weight: 500;
  }

  .verify-code {
    display: flex;
    gap: 10px;

    .code-img {
      height: 40px;
      cursor: pointer;
      border-radius: 4px;
    }
  }

  .agree-item {
    margin: 20px 0;

    a {
      color: #8b4513;
      &:hover {
        text-decoration: underline;
      }
    }
  }

  .login-btn {
    width: 100%;
    height: 45px;
    font-size: 16px;
    margin-top: 10px;
    background: #8b4513;
    border-color: #8b4513;
    &:hover {
      background: #a0522d;
      border-color: #a0522d;
    }
  }

  .login-extra {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    font-size: 14px;

    a {
      color: #8b4513;
      &:hover {
        text-decoration: underline;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #666;
      transition: color 0.2s;

      &:hover {
        color: #8b4513;
      }

      ~ a {
        border-left: 1px solid #ccc;
      }
    }
  }
}
</style>
